"""
自定义分页组件
在视图函数中:
    def pretty(request):
    1.根据自己的情况获取自己筛选的数据
    queryset = models.PrettyNum.objects.all()
    2.实例化分页组件
    page_object = Pagination(request,queryset)
    context={
        "queryset":page_object.page_queryset, #分页数据
        "page_string":page_object.html()      #生成页码
    }
    return render(request,'pretty_list.html',context')

在html页面中
    {% for obj in queryset %}
    {{obj.xx}}
    {% endfor %}
    <ul class="pagination" style="float: left">
            {{ page_string }}
    </ul>
"""
from django.utils.safestring import mark_safe
import copy
class Pagination(object):
    def __init__(self,request,queryset,page_size=10,page_param="page", plus = 5):
        query_dict = copy.deepcopy(request.GET)
        query_dict._mutable = True
        self.query_dict=query_dict
        self.page_param=page_param
        page=request.GET.get(page_param,"1")

        if page.isdecimal():
            page= int(page)
        else:
            page=1
        self.page=page
        self.page_size=page_size
        self.start = (page - 1) * page_size
        self.end = page * page_size
        self.page_queryset=queryset[self.start:self.end]

        tatal_count = queryset.count()
        tatal_page_count, div = divmod(tatal_count, page_size)
        if div:
            tatal_page_count += 1
        self.tatal_page_count=tatal_page_count
        self.plus=plus

    def html(self):
        # 计算出每页显示前后5个翻页
        # page是当前页
        if self.tatal_page_count <= 2 * self.plus + 1:
            # 数据较少没有达到11页
            start_page = 1
            end_page = self.tatal_page_count
        else:
            # 数据库较多超过11页
            # 当前页小于5
            if self.page <= self.plus:
                start_page = 1
                end_page = 2 * self.plus + 1
            else:
                # 当前页大于5
                if (self.page + self.plus) >= self.tatal_page_count:
                    start_page = self.tatal_page_count - 2 * self.plus
                    end_page = self.tatal_page_count
                else:
                    start_page = self.page - self.plus
                    end_page = self.page + self.plus + 1

            # 根据条件查询数据库的数据
        page_str_list = []
        self.query_dict.setlist(self.page_param, [1])

        # 首页
        page_str_list.append('<li><a href="?{}">首页</a></li>'.format(self.query_dict.urlencode()))
        # 上一页
        if self.page > 1:
            self.query_dict.setlist(self.page_param, [self.page - 1])
            prev = '<li><a href="?{}">上一页</a></li>'.format(self.query_dict.urlencode())
        else:
            self.query_dict.setlist(self.page_param, [1])
            prev = '<li><a href="?{}">上一页</a></li>'.format(self.query_dict.urlencode())
        page_str_list.append(prev)
        for i in range(start_page, end_page + 1):
            self.query_dict.setlist(self.page_param, [i])
            if i == self.page:
                ele = '<li class="active"><a href="?{}">{}</a></li>'.format(self.query_dict.urlencode(), i)
            else:
                ele = '<li><a href="?{}">{}</a></li>'.format(self.query_dict.urlencode(), i)
            page_str_list.append(ele)
        # 下一页
        if self.page < self.tatal_page_count:
            self.query_dict.setlist(self.page_param, [self.page + 1])
            next = '<li><a href="?{}">下一页</a></li>'.format(self.query_dict.urlencode())
        else:
            self.query_dict.setlist(self.page_param, [self.tatal_page_count])
            next = '<li><a href="?{}">下一页</a></li>'.format(self.query_dict.urlencode())
        page_str_list.append(next)
        # 尾页
        self.query_dict.setlist(self.page_param, [self.tatal_page_count])
        page_str_list.append('<li><a href="?{}">尾页</a></li>'.format(self.query_dict.urlencode()))
        search_string = """
                    <li>
                            <form style="float:left;margin-left: -1px" method="get">

                                 <input type="text" name="page" class="form-control" placeholder="页码"
                                 style="position: relative;float: left;display:inline-block; width: 80px;border-radius: 0">

                                    <button style="border-radius: 0" class="btn btn-default" type="submit">跳转</button>


                            </form>
                        </li>
                """
        page_str_list.append(search_string)
        page_string = mark_safe("".join(page_str_list))
        return page_string

